<?php
// +----------------------------------------------------------------------
// | NewThink [ Think More,Think Better! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +----------------------------------------------------------------------
// | 版权所有：山西创泰网络科技有限公司
// +----------------------------------------------------------------------
// | Author: 燕南天 QQ:81020302 Date:2018-08-01 Time:17:48
// +----------------------------------------------------------------------

namespace app\api\service;

use app\base\service\center\OptionsCenter;
use mikkle\tp_tools\ShowCode;
use app\base\model\terminal\Terminal as TerminalModel;

class Terminal extends ServiceBase
{
    public function _initialize()
    {
        $this->model = new TerminalModel();
    }
    
    public function terminalRegister($data){
        $this->functionName = __FUNCTION__;
        $this->args = $data;
        if (empty($data[OptionsCenter::$fieldTerminalCode])){
            $this->addError( "设备码参数丢失");
            return ShowCode::jsonCodeWithoutData(1003,$this->error);
        };
        $map = [
            OptionsCenter::$fieldTerminalCode =>$data[OptionsCenter::$fieldTerminalCode],
        ];
        
        $terminal = $this->getInfoObject($map);
        if (empty($terminal)){
            $this->addError ("设备码不存在");
            return ShowCode::jsonCodeWithoutData(1010,$this->error );
        }
        if ((int)$terminal[OptionsCenter::$fieldTerminalStatus] == 0){
            $this->addError ("设备未使用状态");
            return ShowCode::jsonCodeWithoutData(1010,$this->error);
        }
        switch (true) {
            case (empty($terminal[OptionsCenter::$fieldTerminalMac])):
                $terminal->save([OptionsCenter::$fieldTerminalMac => $data[OptionsCenter::$fieldTerminalMac]]);
                return ShowCode::jsonCode(1001, ["type" => "add"], "设备添加成功");
                break;
            case ($terminal[OptionsCenter::$fieldTerminalMac] == $data[OptionsCenter::$fieldTerminalMac]):
                return ShowCode::jsonCode(1001, ["type" => "reg"], "设备注册成功");
                break;
            default:
                $this->addError ( "设备机身码不符");
                return ShowCode::jsonCode(1001, ["type" => "differ"], "设备机身码不符");
        }
        
    }
    
    public function terminalSign($data){
        $this->functionName = __FUNCTION__;
        $this->args = $data;
        if (empty($data[OptionsCenter::$fieldTerminalMac])){
            $this->addError ( "设备终端号参数丢失");
            return ShowCode::jsonCodeWithoutData(1003,$this->error);
        };
        $map = [
            OptionsCenter::$fieldTerminalMac=>$data[OptionsCenter::$fieldTerminalMac],
        ];
        
        $terminal = $this->getInfoArray($map);
        if (empty($terminal)){
            $this->addError ( "设备不存在");
            return ShowCode::jsonCodeWithoutData(1010,$this->error );
        }
        
        if ((int)$terminal["terminal_status"] == 0){
            $this->addError ( "设备未使用状态");
            return ShowCode::jsonCodeWithoutData(1010,"设备未使用状态");
        }
        $result["park_list"]=$this->model->getTerminalParkList($terminal[OptionsCenter::$pk]);
        return ShowCode::jsonCode(1001,$result);
        
    }
    
}
